package cn.sunshine.ymcc.service.impl;

import cn.sunshine.ymcc.domain.CourseMarket;
import cn.sunshine.ymcc.domain.CourseUserLearn;
import cn.sunshine.ymcc.dto.PayAliNotifyDto;
import cn.sunshine.ymcc.mapper.CourseUserLearnMapper;
import cn.sunshine.ymcc.result.JSONResult;
import cn.sunshine.ymcc.service.ICourseMarketService;
import cn.sunshine.ymcc.service.ICourseUserLearnService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr.Jiang
 * @since 2022-08-08
 */
@Service
public class CourseUserLearnServiceImpl extends ServiceImpl<CourseUserLearnMapper, CourseUserLearn> implements ICourseUserLearnService {
    @Autowired
    private ICourseMarketService courseMarketService;


    @Override
    public JSONResult checkUserpermission(Long loginId, Long courseId) {
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("login_id", loginId);
        wrapper.eq("course_id", courseId);
        CourseUserLearn courseUserLearn = selectOne(wrapper);
        if (courseUserLearn == null) {
            return JSONResult.error("您还没购买这个课程");
        }
        return JSONResult.success();
    }

    /*
     * @Title: upDateUserByNotify
     * @Description: 修改用户购买课程状态
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/8/22 20:49
     * @Parameters: [notifyDto]
     * @Return void
     */
    @Override
    public void upDateUserByNotify(PayAliNotifyDto notifyDto) {
        // 1.根据扩展参数得到loginId + courseId
        Map<String, Object> map = JSONObject.parseObject(notifyDto.getExtParams(), Map.class);
        // 2.根据loginId和courseId查询用户的购买课程记录
        Long loginId = Long.valueOf(map.get("loginId").toString());
        String courseIdStr = map.get("courseId").toString(); // 可能有多个
        // 多个就分隔
        String[] courseIds = courseIdStr.split(",");
        Date now = new Date();
        // 遍历
        for (String id : courseIds) {
            Long courseId = Long.valueOf(id);
            // 根据loginId和courseId查询课程记录
            CourseUserLearn courseUserLearn = selectByLoginIdAndCourseId(loginId, courseId);
            CourseMarket courseMarket = courseMarketService.selectById(courseId);
            // 3.查询没有记录就直接新增
            if (courseUserLearn == null) {
                CourseUserLearn learn = new CourseUserLearn();
                learn.setLoginId(loginId);
                learn.setStartTime(now);
                // 增加时长。
                learn.setEndTime(DateUtils.addDays(now, courseMarket.getValidDays()));
                learn.setState(0);
                learn.setCourseId(courseId);
                learn.setCourseOrderNo(notifyDto.getOrderNo());
                learn.setCreateTime(now);
                insert(learn);
            } else {
                // 4.查询到了
                //  4.1 增加观看时间并且保存最新的下单编号
                courseUserLearn.setCourseOrderNo(notifyDto.getOrderNo());
                // 这个地方应该判断一下课程是否过期，过期就从现在开始算时间，没有过期就从之前的结束时间往上加
                courseUserLearn.setEndTime(DateUtils.addDays(courseUserLearn.getEndTime(), courseMarket.getValidDays()));
                updateById(courseUserLearn);
            }
        }
    }

    // 根据loginId和courseId查询课程记录
    public CourseUserLearn selectByLoginIdAndCourseId(Long loginId, Long courseId) {
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("login_id", loginId);
        wrapper.eq("course_id", courseId);
        return selectOne(wrapper);
    }


}
